package com.ruoyi.web.controller.business;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtDoctorArticle;
import com.ruoyi.business.service.IHurtDoctorArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 医师文章Controller
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/business/doctorArticle")
public class HurtDoctorArticleController extends BaseController
{
    @Autowired
    private IHurtDoctorArticleService hurtDoctorArticleService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询医师文章列表
     */
    @PreAuthorize("@ss.hasPermi('business:doctorArticle:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtDoctorArticle hurtDoctorArticle)
    {
        startPage();
        List<HurtDoctorArticle> list = hurtDoctorArticleService.selectHurtDoctorArticleList(hurtDoctorArticle);
        return getDataTable(list);
    }

    /**
     * 导出医师文章列表
     */
    @PreAuthorize("@ss.hasPermi('business:doctorArticle:export')")
    @Log(title = "医师文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtDoctorArticle hurtDoctorArticle)
    {
        List<HurtDoctorArticle> list = hurtDoctorArticleService.selectHurtDoctorArticleList(hurtDoctorArticle);
        ExcelUtil<HurtDoctorArticle> util = new ExcelUtil<HurtDoctorArticle>(HurtDoctorArticle.class);
        util.exportExcel(response, list, "医师文章数据");
    }

    /**
     * 获取医师文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:doctorArticle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtDoctorArticleService.selectHurtDoctorArticleById(id));
    }

    /**
     * 新增医师文章
     */
    @PreAuthorize("@ss.hasPermi('business:doctorArticle:add')")
    @Log(title = "医师文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtDoctorArticle hurtDoctorArticle)
    {
        return toAjax(hurtDoctorArticleService.insertHurtDoctorArticle(hurtDoctorArticle));
    }

    /**
     * 修改医师文章
     */
    @PreAuthorize("@ss.hasPermi('business:doctorArticle:edit')")
    @Log(title = "医师文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtDoctorArticle hurtDoctorArticle)
    {
        return toAjax(hurtDoctorArticleService.updateHurtDoctorArticle(hurtDoctorArticle));
    }

    /**
     * 删除医师文章
     */
    @PreAuthorize("@ss.hasPermi('business:doctorArticle:remove')")
    @Log(title = "医师文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtDoctorArticleService.deleteHurtDoctorArticleByIds(ids));
    }

    /**
     * 获取医师文章列表
     * @return
     */
    @GetMapping("/getDoctorArticleList")
    public TableDataInfo getDoctorArticleList(@RequestParam(required = false) String title){
        startPage();
        List<Map<String, Object>> list = hurtDoctorArticleService.getDoctorArticleList(title);
        return getDataTable(list);
    }

    /**
     * 获取医师文章内容
     * @param id
     * @return
     */
    @GetMapping("/readArticleById")
    public AjaxResult readArticleById(@RequestParam Long id){
        //增加文章阅读量
        hurtDoctorArticleService.updateClickNum(id);
        Map<String, Object> map = hurtDoctorArticleService.readArticleById(id);
        return AjaxResult.success(map);
    }

    /**
     * 医师获取自己发布的文章列表
     * @return
     */
    @GetMapping("/getDoctorOwnArticleList")
    public AjaxResult getDoctorOwnArticleList(){
        Long doctorId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = hurtDoctorArticleService.getDoctorOwnArticleList(doctorId);
        return AjaxResult.success(list);
    }


    /**
     * 医师发布文章
     * @param hurtDoctorArticle
     * @return
     */
    @PostMapping("/releaseArticle")
    public AjaxResult releaseArticle(@RequestBody HurtDoctorArticle hurtDoctorArticle){
        Long doctorId = SecurityUtils.getUserId();
        hurtDoctorArticle.setDoctorId(doctorId);
        return toAjax(hurtDoctorArticleService.insertHurtDoctorArticle(hurtDoctorArticle));
    }

    /**
     * 上传文章封面图片
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadArticleCover")
    public AjaxResult uploadArticleCover(@RequestParam("articleCover") MultipartFile file) throws Exception{
        System.out.println("文章封面="+file);
        if (!file.isEmpty())
        {
            String cover = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            System.out.println("上传成功地址:"+cover);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", serverConfig.getUrl()+"/prod-api"+cover);
            return ajax;
        }
        return error("上传异常");
    }

    /**
     * 获取医师文章详情
     * @return
     */
    @GetMapping("/getArticleDetail")
    public AjaxResult getArticleDetail(Long id){
        Map<String, Object> map = hurtDoctorArticleService.getArticleDetail(id);
        return AjaxResult.success(map);
    }
}
